package sdk.org.apache.shiro.G2_使用.C06_授权;

import java.util.Arrays;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

import sdk.org.apache.shiro.utils.ShiroUtils;

public class 验证角色 {
    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run(timer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print("执行总用时");
            System.exit(0);
        }
    }

    
    private static void run(Timer timer) throws Exception {
        ShiroUtils.init(
              new AuthorizingRealm() {
                protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
                    ConsoleUtils.log("doGetAuthorizationInfo...");
                    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
                    authorizationInfo.addRole("role1");
                    authorizationInfo.addRole("role2");
                    return authorizationInfo;
                }
                protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
                    String username = (String)token.getPrincipal();  //得到用户名
                    String password = new String((char[])token.getCredentials()); //得到密码
                    ConsoleUtils.log("doGetAuthenticationInfo", "username", username, "password", password);
                    if(!"zhang".equals(username) || !"123".equals(password)) {
                        throw new UnknownAccountException();
                    }
                    return new SimpleAuthenticationInfo(username, password, getName());
                }
              });
        
        Subject currentUser = ShiroUtils.login("zhang", "123", true);
        //判断拥有角色：role1
        ConsoleUtils.log("hasRole(\"role1\")", currentUser.hasRole("role1"));
        ConsoleUtils.log("hasAllRoles(Arrays.asList(\"role1\", \"role2\"))", currentUser.hasAllRoles(Arrays.asList("role1", "role2")));
        boolean[] result = currentUser.hasRoles(Arrays.asList("role1", "role2", "role3"));
        ConsoleUtils.log("是否有角色role1", result[0]);
        ConsoleUtils.log("是否有角色role2", result[1]);
        ConsoleUtils.log("是否有角色role3", result[2]);
        //断言拥有角色：role1
        currentUser.checkRole("role1");
        try {
            //断言拥有角色：role1 and role3 失败抛出异常
            currentUser.checkRoles("role1", "role3");
        } catch (UnauthorizedException e) {
            System.err.println(e.toString());
        }
        
        ShiroUtils.logout();
    }
}
